import encode from "./encoding.js";

const encode2 = encode();
class Printer {
	constructor(mode,) {
		this.mode = mode;
	}

	command = []
	textEncoder = new encode2.TextEncoder('gb18030', { NONSTANDARD_allowLegacyEncoding: true })
	// Unicode,gb2312,cp936,GBK,GB18030

	addCommand(content) {
		//#ifdef MP-WEIXIN
		const code = this.textEncoder.encode(content)
		for (var i = 0; i < code.length; ++i) {
			this.command.push(code[i])
		}
		//#endif
		
		//#ifdef APP-PLUS
		let byteCommand = plus.android.invoke(content, 'getBytes', 'gb18030');
		for (var i = 0; i < byteCommand.length; ++i) {
			this.command.push(byteCommand[i])
		}
		//#endif
	}

	getCommand() {
		return this.command;
	}

	print() {
		setPrint = () => {
			jpPrinter.addCommand("PRINT\r\n")
		}
	}
}


class CpclPrint extends Printer {
	constructor(mode) {
		super(mode)
	}

	setPage(offset=0,wDpi=576,hDpi=250,mhDpi=250,qty=1) {
		this.addCommand(`! ${offset} ${wDpi} ${hDpi} ${mhDpi} ${qty}\r\n`);
	}
	/**
	 * @font 字体
	 * @size 尺寸
**/
	addText(font = 3, size = 1, x, y, text) {
		this.addCommand(`TEXT ${font} ${size} ${x} ${y} ${text}\r\n`)
	}

	/**
	 * @type 格式
	 * 
**/
	addBarcode(type = 39, width = 1, ratio = 1, height = 50, x, y, code) {
		this.addCommand("BARCODE-TEXT OFF\r\n")
		this.addCommand(`BARCODE ${type} ${width} ${ratio} ${height} ${x} ${y} ${code}\r\n`)
	}
	
    addCls() {
		this.addCommand("CLS \r\n")
	}
	
	
	addForm() {
		this.addCommand("FORM\r\n")
	}

	addPrint() {
		this.addCommand("PRINT\r\n")
	}
}

class TsplPrint extends Printer {
	constructor(mode) {
		super(mode)
	}
}

export default CpclPrint